const {
  $util,
} = getApp();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    genderarr: [{
      id: 1,
      name: '男',
    }, {
      id: 2,
      name: '女',
    }],
    showPop: false,
    nationarr: ['汉族', '蒙古族', '回族', '藏族', '维吾尔族', '苗族', '彝族', '壮族', '布依族', '朝鲜族', '满族', '侗族', '瑶族', '白族', '土家族', '哈尼族', '哈萨克族', '傣族', '黎族', '傈僳族', '佤族', '畲族', '高山族', '拉祜族', '水族', '东乡族', '纳西族', '景颇族', '柯尔克孜族', '土族', '达斡尔族', '仫佬族', '羌族', '布朗族', '撒拉族', '毛南族', '仡佬族', '锡伯族', '阿昌族', '普米族', '塔吉克族', '怒族', '乌孜别克族', '俄罗斯族', '鄂温克族', '德昂族', '保安族', '裕固族', '京族', '塔塔尔族', '独龙族', '鄂伦春族', '赫哲族', '门巴族', '珞巴族', '基诺族'],
    degree: -1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    $util.request({
      url: 'entry/wxapp/partyman.mine.partymaninfo',
      data: {},
      showLoading: true,
      success: (res) => {
        res.data.gender = res.data.sex === '1' ? 0 : 1;
        res.data.nat = this.data.nationarr.findIndex(item => res.data.nation === item);
        res.data.opt = res.operating.findIndex(item => Number(res.data.operatingid) === item.id);
        res.data.operating = res.operating;
        res.data.degreearr = res.degree;
        res.data.degree = res.degree.findIndex(item => Number(res.data.degree) == item.id);
        this.setData(res.data);
      },
    });
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    })
    // 获取裁剪图片
    let resPic = wx.getStorageSync('resPic');
    typeof resPic === 'string' && resPic !== '' && this.setData({ avator: resPic });
  },
  bindInput(e) {
    this.setData({
      flowarea: e.detail.value,
    });
  },
  showPop() {
    this.setData({
      showPop: true,
    });
  },
  getUserMobile(e) {
    this.setData({
      phone: e.detail.mobile,
    });
    wx.hideLoading();
  },
  bindDateChange(e) {
    const {
      typ,
    } = e.currentTarget.dataset;
    const m = {};
    m[typ] = e.detail.value;
    this.setData(m);
  },
  changeAvator() {
    // 上传并裁剪图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '加载中',
          mask: false,
        });
        let tempFilePaths = res.tempFilePaths;
        let url = 'utility/file/upload';
        if (url.indexOf('http://') === -1 && url.indexOf('https://') === -1) {
          url = app.util.url(url, { type: 'image', thumb: '0' });
        }
        wx.setStorage({
          key: 'uploadUrl',
          data: url,
        });
        wx.hideLoading();
        app.util.navigateTo(`/common/component/cropper/cropper?sceneName=card&src=${tempFilePaths[0]}`);
      },
    });
  },
  btnPrevent: false,
  submit() {
    const {
      avator,
      birthday,
      nat,
      nationarr,
      genderarr,
      gender,
      opt,
      operating,
      flowarea,
      phone,
      province,
      city,
      area,
      degree,
      degreearr,
      specialty,
      professional,
      relativeperson,
    } = this.data;

    let tel = /^([a-zA-Z0-9\u4e00-\u9fa5\·]{1,10})(-)(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;

    if (relativeperson.trim() !== '') {
    if (!tel.test(relativeperson)) {
      return wx.showToast({
        title: '第二联系人输入有误',
        icon: 'none',
        duration: 1500,
        mask: false,
      });
      }
    }

    if (this.btnPrevent) return;
    this.btnPrevent = true;
  
    $util.request({
      url: 'entry/wxapp/partyman.mine.saveinfo',
      data: {
        avator,
        birthday,
        nation: -1 !== nat && nationarr[nat] || '',
        sex: -1 !== gender && genderarr[gender].id || '',
        operatingid: -1 !== opt && operating[opt].id || '',
        flowarea,
        phone,
        province,
        city,
        area,
        degree: -1 !== degree && degreearr[degree].id || '',
        specialty,
        professional,
        relativeperson,
      },
      success: () => {
        this.btnPrevent = false;
        wx.showToast({
          title: '修改成功',
          icon: 'none',
          success: () => {
            setTimeout(() => {
              wx.navigateBack();
            }, 1000);
          },
        });
      },
    });
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1],
      area: e.detail.value[2],
    })
  },
  onUnload() {
    // 页面卸载时清除图片resPic及uploadUrl换成
    wx.removeStorageSync('resPic');
    wx.removeStorageSync('uploadUrl');
    wx.removeStorageSync('cuttedImg');
  },
});